package com.gitee.wsl.platform.concurrent

import platform.Foundation.NSThread
import platform.darwin.dispatch_async
import platform.darwin.dispatch_get_main_queue
import kotlinx.coroutines.CloseableCoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
//
//@OptIn(ExperimentalCoroutinesApi::class)
//internal actual class InternalThreadId(val nThread:NSThread)
//
//internal actual class InternalThread {
//     actual companion object {
//        actual fun getId(): InternalThreadId = InternalThreadId(NSThread.currentThread())
//        actual fun sleepMs(millis: Int) { }
//     }
//}

//public  val platformThread: PlatformThread = PlatformThread(
//    thread = NSThread.currentThread(),
//    id = NSThread.currentThread().threadPriority.toString(),
//    name = NSThread.currentThread().name.orEmpty(),
//)


/**
 * Runs the provided [block] on the main thread
 * @param block the code to run on the main thread.
 */
actual fun runOnMain(block: () -> Unit) {
    dispatch_async(dispatch_get_main_queue(), block)
}

/**
 * Creates a coroutine execution context using a single thread.
 *
 * **NOTE: The resulting [CloseableCoroutineDispatcher] owns native resources (its thread).
 * Resources are reclaimed by [CloseableCoroutineDispatcher.close].**
 * @param name The name of the thread to be created.
 */
//@OptIn(DelicateCoroutinesApi::class, ExperimentalCoroutinesApi::class)
//actual fun singleThreadDispatcher(name: String): CloseableCoroutineDispatcher = newSingleThreadContext(name)

/**
 * Creates a coroutine execution context using a thread pool.
 *
 * **NOTE: The resulting [CloseableCoroutineDispatcher] owns native resources (its thread).
 * Resources are reclaimed by [CloseableCoroutineDispatcher.close].**
 */
//@OptIn(DelicateCoroutinesApi::class)
//actual fun threadPoolDispatcher(numberOfThreads: UInt, name: String): CloseableCoroutineDispatcher = newFixedThreadPoolContext(numberOfThreads.toInt(), name)